package com.zx.idc.backend.gui.api.management.basicdata;

import com.zx.idc.ds.slog.entity.SlogAbnormalIp;
import com.zx.idc.ds.slog.entity.SlogIllegalSite;
import com.zx.idc.ds.slog.service.ISlogAbnormalIpService;
import com.zx.idc.ds.slog.service.ISlogIllegalSiteService;
import com.zx.idc.xml.entity.idcmonitor.IdcMonitor;
import io.vavr.control.Option;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

/**
 * @author rxliuli
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class BaseIdcMonitorDataService {

    @Autowired
    private ISlogIllegalSiteService slogIllegalSiteService;
    @Autowired
    private ISlogAbnormalIpService abnormalIpService;

    public void update(IdcMonitor idcMonitor, int reported) {
        Option.of(idcMonitor)
            .map(IdcMonitor::getHouseMonitor)
            .filter(Objects::nonNull)
            .peek(houseMonitors -> {
                houseMonitors.stream().forEach(houseMonitor -> {
                    //更新违法网站的上报状态
                    SlogIllegalSite illegalSite = new SlogIllegalSite()
                        .setHouseId(houseMonitor.getId())
                        .setIp(houseMonitor.getIp())
                        .setPort((int)houseMonitor.getPort())
                        .setReportStatus(reported);
                    slogIllegalSiteService.updateReportStatusForResBack(illegalSite);

                    //更新非法ip的上报状态
                    SlogAbnormalIp abnormalIp = new SlogAbnormalIp().
                        setReportStatus(reported)
                        .setIp(houseMonitor.getIp())
                        .setPort((int)houseMonitor.getPort())
                        .setHouseId(houseMonitor.getId());
                    abnormalIpService.updateReportStatusForResBack(abnormalIp);
                });

            });
    }
}
